﻿namespace DesktopRibbon
{
    using System.Windows;
    using Microsoft.Windows.Controls.Ribbon;
    using System.Windows.Data;
    using System;
    using System.ComponentModel;

    /// <summary>
    /// The RibbonElementFactory creates a ribbon element from a launcher (inheriting ILauncherItem) and a DisplayConfiguration.
    /// </summary>
    class RibbonElementFactory
    {
        /// <summary>
        /// Creates a ribbon element from the launcher using the default DisplayConfiguration.
        /// </summary>
        /// <param name="launcher">The launcher that the new ribbon element should be displaying.</param>
        /// <returns>A new ribbon element displaying the specified launcher.</returns>
        public static FrameworkElement Create(Launcher.LauncherItem launcher)
        {
            return Create(launcher, new DisplayConfiguration());
        }

        /// <summary>
        /// Creates a ribbon element from the launcher using the specified DisplayConfiguration.
        /// </summary>
        /// <param name="launcher">The launcher that the new ribbon element should be displaying.</param>
        /// <param name="config">The DisplayConfiguration that should be used to create the ribbon element.</param>
        /// <returns>A new ribbon element displaying the specified launcher, using the specified DisplayConfiguration.</returns>
        public static FrameworkElement Create(Launcher.LauncherItem launcher, DisplayConfiguration config)
        {
            switch(config.DisplayMode)
            {
                case DisplayMode.AsButton:
                    var result = new RibbonButton();
                    launcher.PropertyChanged += (object sender, PropertyChangedEventArgs args) => {
                        var group = (RibbonGroup) Helper.DragDropHelper.FindParentAnyTypeOf(result, new Type[] {typeof(RibbonGroup)});
                        if (null != group)
                        {
                            int index = group.Items.IndexOf(result);
                            group.Items.Remove(result);
                            group.Items.Insert(index, result);
                        }
                    };
                    result.SetBinding(RibbonButton.LabelProperty, new Binding("Label") { Source = launcher });
                    result.SetBinding(RibbonButton.SmallImageSourceProperty, new Binding("SmallImageSource") { Source = launcher });
                    result.SetBinding(RibbonButton.LargeImageSourceProperty, new Binding("LargeImageSource") { Source = launcher });
                    if(launcher.IsLaunchable) {
                        result.Click += (object sender, RoutedEventArgs args) => {
                            launcher.Launch();
                        };
                    }
                    return result;
                case DisplayMode.AsMenuButton:
                    throw new NotImplementedException();
                case DisplayMode.AsSplitButton:
                    throw new NotImplementedException();
                case DisplayMode.AsGallery:
                    throw new NotImplementedException();
                default:
                    throw new NotImplementedException();
            }
        }

    }
}
